<?php

namespace App\Containers\AppSection\Accreditation\Tasks;

use App\Containers\AppSection\Accreditation\Data\Repositories\AccreditationRepository;
use App\Ship\Criterias\Where;
use App\Ship\Parents\Tasks\Task;
use App\Ship\Traits\FilterByDateTrait;

class GetAllAccreditationsTask extends Task
{
    use FilterByDateTrait;

    protected AccreditationRepository $repository;

    public function __construct(AccreditationRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run()
    {
        return $this->repository->paginate();
    }

    public function fromRole($role): self
    {
        $this->repository->whereHas('profile.user.roles', function ($query) use ($role) {
            $query->where('roles.name', $role);
        });

        return $this;
    }

    public function withProfileStatus($status): self
    {
        $this->repository->whereHas('profile', function ($query) use ($status) {
            $query->where('accreditation_status_key', $status);
        });

        return $this;
    }

    public function withStage($stage): self
    {
        $this->repository->pushCriteria(new Where('stage', '=', $stage));

        return $this;
    }

    public function forProfile($profileId): self
    {
        $this->repository->pushCriteria(new Where('profile_id', '=', $profileId));

        return $this;
    }

    public function active(): self
    {
        $this->repository->has('profiles.accreditation');

        return $this;
    }
}
